﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Dubbo.Common.Utils
{
    public sealed class TypeUtils
    {
        public static Type GetType(string typeName)
        {
            throw new NotImplementedException();
        }

        public static MethodInfo GetMethod(Type type, string name, Type[] types)
        {
            if (type.IsInterface)
            {
                return GetMethodByRecursiveInterface(type, name, types);
            }
            else
            {
                return type.GetMethod(name, types);
            }
        }

        private static MethodInfo GetMethodByRecursiveInterface(Type type, string name, Type[] types)
        {
            if (!type.IsInterface)
            {
                throw new ArgumentException("Type: " + type + " is not interface!");
            }
            var method = type.GetMethod(name, types);
            if (method == null && type.GetTypeInfo().ImplementedInterfaces.Count() != 0)
            {
                foreach(var iface in type.GetTypeInfo().ImplementedInterfaces)
                {
                    method = GetMethodByRecursiveInterface(iface, name, types);
                    if (method != null)
                    {
                        return method;
                    }
                }
            }
            return method;
        }
    }
}
